<%@page import="nl.hva.web.mvcwebapp.models.News"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@include file="/WEB-INF/templates/configuration.jsp" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%
    HttpSession session_user = request.getSession(true);
    String firstname = (String) session_user.getAttribute("user_firstname");
    int user_id = 0;
    int user_type = 0;
    if (firstname != null) {
        user_id = Integer.valueOf("" + session_user.getAttribute("user_id"));
        user_type = Integer.valueOf("" + session_user.getAttribute("user_type"));
    }

%>
<h2>${pageTitle}</h2>
<c:choose>
    <c:when test="${aantalNieuws != 0}">
        <!-- Wanneer er gebruikers opgeslagen zijn, worden ze hier getoond -->
        <table class="zebra">
            <tr>
                <td>
                    <strong>News Title</strong>
                </td>
                <td>
                    <strong>News Content</strong>
                </td>
                <td>
                    <strong>News Date</strong>
                </td>
                <td></td>
            </tr>
            <c:forEach var="tempNieuws" items="${nieuwsUitSessie}">
                <!-- Per gebruiker wordt nu een rij aangemaakt met daarin zijn gegevens -->
                <tr>
                    <td>
                        ${tempNieuws.newstitle}
                    </td>

                    <%
                        String url = (String) pageContext.getAttribute("url");
                        News temp_news = (News) pageContext.getAttribute("tempNieuws");
                        String temp_content = temp_news.getNewscontent();
                        int temp_news_id = temp_news.getNewsid();
                        if (temp_content.length() >= 600) {
                            temp_content = temp_content.substring(0, 600);
                            temp_content += "...<br/></p><a href=" + url + "news/view?newsid=" + temp_news_id + ">Continue reading..</a>";
                        } else {
                        }
                    %>
                    <td class="news-item-content">
                        <%=temp_content%>
                    </td>
                    <td>
                        ${tempNieuws.newsdate}
                    </td>
                    <%if (user_type == 2 || user_type == 3) {
                    %>
                    <td>
                        <a  class="icon edit" href="${url}news/edit?newsid=${tempNieuws.newsid}">edit</a> 
                        <a  class="icon view" href="${url}news/view?newsid=${tempNieuws.newsid}">view</a> 
                        <a  class="icon delete" href="${url}news/delete?newsid=${tempNieuws.newsid}">delete</a>
                        <!--                                <a href="javascript:if(confirm('Weet u zeker dat u dit nieuws wil verwijderen?'))
                                                           window.location='news/delete?newsid=${tempNieuws.newsid}';">delete</a>-->
                    </td>
                    <%}%>
                </tr>
            </c:forEach>
        </table>
    </c:when>
    <c:otherwise>
        <!-- Als er geen gebruikers zijn, wordt deze melding getoond -->
        There is no news found.
    </c:otherwise>
</c:choose>
<%if (user_type == 2 || user_type == 3) {
%>
<p>
    <!--News course overview-->       
<!--    <a href="${url}news/view/course?courseid=1">view</a>-->

    <a class="button" href="${url}news/new">Add a new News item</a>
</p>

<%            }%>
